//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web;
using System.Web.UI;
using CommunityServer.Components;

namespace CommunityServer.Controls
{

	// *********************************************************************
	//  HelpIcon
	//
	/// <summary>
	/// Renders text with a DHTML MouseOver tooltip
	/// </summary>
	// ********************************************************************/ 
	public class HelpIcon : LiteralControl 
	{
		
		public HelpIcon()
		{
		}

		protected string _text = null;
		protected string _resourceName = null;
		protected string _resourceFile = null;

		/// <summary>
		/// Property CSSClass (string)
		/// </summary>
		[System.ComponentModel.DefaultValue( "helpicon" )]
		public virtual String cssclass 
		{
			get 
			{
				Object state = ViewState["cssclass"];
				if ( state != null ) 
				{
					return (String)state;
				}
				return "helpicon";
			}
			set 
			{
				ViewState["cssclass"] = value;
			}
		}
		
		public override string Text
		{
			get{return _text;}
			set { _text = value;}
		}

		public string ResourceName
		{
			get { return _resourceName;}
			set { _resourceName = value;}
		}
				
		public string ResourceFile
		{
			get { return _resourceFile;}
			set { _resourceFile = value;}
		}
		
		[Obsolete("TipResourceName is obsolete, use the ResourceName property")]
		public string TipResourceName
		{
			get{return _resourceName;}
			set { _resourceName = value;}
		}

		protected override void Render(HtmlTextWriter writer)
		{

			//Dont write them if the user disabled help icons
			if(!Globals.SafeBool(CSContext.Current.User.GetExtendedAttribute("CPShowHelpIcons"),true))
				return;


			writer.Write(@"<a href=""""");
			writer.Write(" id=\"{0}\"", this.ClientID);

			string tipText = Globals.IsNullorEmpty(this.Text) ? GetResourceString() : this.Text;

			writer.Write(@" class=""{0}""  onfocus=""if(this.blur)this.blur();""  TabIndex=""-1""  onClick=""{1}"" ", this.cssclass, "event.cancelBubble=true;return(false);");

			if(tipText != null)
				writer.Write(@" onMouseOver=""return escape('{0}')""", HttpUtility.HtmlAttributeEncode(tipText.Replace("'", @"\'")));


			writer.Write(@"><img src=""{0}/images/spacer.gif"" class=""{1}"" border=""0"" ></a>", Globals.GetSkinPath(), this.cssclass);


		
		}

		protected override void OnPreRender(EventArgs e)

		{
			if(!Page.IsClientScriptBlockRegistered("hoverhelper"))
			{
				string clientScript = string.Concat(@"<script language=""JavaScript"" type=""text/javascript"" src=""", SiteUrls.Instance().Locations["utility"]  ,@"wz_tooltip.js""></script>");
				Page.RegisterStartupScript("hoverhelper",clientScript);
			}
		}

		protected virtual string GetResourceString()
		{
			if (Globals.IsNullorEmpty(_resourceFile))
				return CommunityServer.Components.ResourceManager.GetString(_resourceName);
			else
				return CommunityServer.Components.ResourceManager.GetString(_resourceName, _resourceFile);
		}

	}
}
